package tests;
import java.util.*;

import client.Client;
import server.*;
import server.data.*;

import dictionary.data.LocationIndex;
import dictionary.gameEntities.*;
import dictionary.gameEntities.Card.CardType;

class MerlinClueLess {

	static String serverIPaddress = "localhost";
	static String clientIPaddress = "localhost";
	static int port = 9999;

	private static List<Thread> threads;

	public static void main(String[] args) {

		class ClientThread extends Thread
		{
			public Client client;
			
			@Override
			public void run() {
				client = new Client(clientIPaddress, port);
			}
		}

		List<Location> allLocations = LocationIndex.getLocations();
		Location player1Location = allLocations.get(0);
		Location player2Location = allLocations.get(1);
		Location player3Location = allLocations.get(allLocations.size()-1);

		CardIndex ci = new CardIndex();
		List<Card> allCards = ci.getCards();
		
		RoomCard arbitraryRoom = getArbitraryRoom(allCards);
		
		CharacterCard arbitrarySuspect = getArbitrarySuspect(allCards);
		
		WeaponCard arbitraryWeapon = getArbitraryWeapon(allCards);
		
		System.out.println("Begining MerlinClueLess Execution...");

		System.out.print("Starting Server...");

		threads = new ArrayList<Thread>();
		
		System.out.print("Starting Server ...");
		Thread server = new Thread() {
			public void run() {
				new Server();
			};
		};
		server.start();
		System.out.println("....completed");
		threads.add(server);
		
		sleep();

		System.out.print("Starting Client 1...");
		ClientThread c1 = new ClientThread();
		c1.start();
		threads.add(c1);
		sleep(5000L);
		System.out.println("....completed");

		System.out.print("Starting Client 2...");
		ClientThread c2 = new ClientThread();
		c2.start();
		threads.add(c2);
		System.out.println("....completed");

		System.out.print("Starting Client 3...");
		ClientThread c3 = new ClientThread();
		c3.start();
		threads.add(c3);
		System.out.println("....completed");

		sleep(5000);
		System.out.print("Moving Client 1...");
		movePlayer(c1.client, player1Location);
		System.out.println("....completed");
		sleep(100L);

		System.out.print("Moving Client 2...");
		movePlayer(c2.client, player2Location);
		System.out.println("....completed");

		sleep(100L);
		System.out.print("Moving Client 3...");
		movePlayer(c3.client, player3Location);
		System.out.println("....completed");

		sleep();
		System.out.println("arbitraryRoom: " + arbitraryRoom.toString());
		System.out.println("arbitrarySuspect: " + arbitrarySuspect.toString());
		System.out.println("arbitraryWeapon: " + arbitraryWeapon.toString());

		sleep();
//		System.out.print("Try Accusation for Client 1...");
//		c1.client.tryAccusation(arbitraryRoom, arbitrarySuspect,
//				arbitraryWeapon);
//		System.out.println("....completed");
//
//		sleep();
//		System.out.print("Try Suggestion for Client 2...");
//		c2.client.trySuggestion(arbitraryRoom, arbitrarySuspect,
//				arbitraryWeapon);
//		System.out.println("....completed");
//
//		sleep();
//		System.out.print("Try Disproval for Client 3...");
//		c3.client.tryDisproval(arbitraryRoom);// , arbitrarySuspect,
//													// arbitraryWeapon)
//		System.out.println("....completed");

		sleep();
		for (Thread t : threads) {
			t.interrupt();
		}
		System.out.println("MerlinClueLess Completed Successfully.");
		System.exit(0);

	}

	private static WeaponCard getArbitraryWeapon(List<Card> allCards) {
		for (int k = 0; k < allCards.size(); k++) {
			if (allCards.get(k).getCardType() == CardType.WEAPON) {
				return (WeaponCard) allCards.get(k);
			}
		}
		return null;
	}

	private static CharacterCard getArbitrarySuspect(List<Card> allCards) {
		for (int j = 0; j < allCards.size(); j++) {
			if (allCards.get(j).getCardType() == CardType.CHARACTER) {
				return (CharacterCard) allCards.get(j);
			}
		}
		return null;
	}

	private static RoomCard getArbitraryRoom(List<Card> allCards) {
		for (int i = 0; i < allCards.size(); i++) {
			if (allCards.get(i).getCardType() == CardType.ROOM) {
				return (RoomCard) allCards.get(i);
			}
		}
		return null;
	}

	private static void sleep() {
		sleep(1000L);
	}

	private static void sleep(long in) {
		try {
			Thread.sleep(in);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private static void movePlayer(Client cl, Location l) {
		Location newLoc = null;
		if (l.getRight() != null)
			newLoc = l.getRight();
		else if (l.getLeft() != null)
			newLoc = l.getLeft();
		else if (l.getDown() != null)
			newLoc = l.getDown();
		else
			newLoc = l.getUp();

		System.out.println("Displaying move to: " + newLoc);
//		cl.moveToLocation(newLoc);
	}
}
